package com.example.gramediamajalahmobile.favorite;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;

import com.example.gramediamajalahmobile.R;
import com.example.gramediamajalahmobile.sqlite.DBHelperFavorite;

public class FavoriteList extends Activity implements OnItemClickListener {
	public static final String KEY_ID = "_id";
	// private static final int INSERT_ID = Menu.FIRST;
	EditText empty;
	private ListView listview;
	private DBHelperFavorite db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listviewfavorite);
		db = new DBHelperFavorite(this);
		db.open();

		listview = (ListView) findViewById(R.id.list);
		listview.setEmptyView(findViewById(R.id.empty));
		listview.setOnItemClickListener(this);

		loadData();
	}

	private void loadData() {
		// TODO Auto-generated method stub
		Cursor cur = db.getAllArticle();
		String[] from = new String[] { DBHelperFavorite.COL_TITLE };
		int[] to = new int[] { R.id.title_f };
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.rowfavorite, cur, from, to);
		listview.setAdapter(adapter);
	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu)
	// {
	// // Inflate the menu; this adds items to the action bar if it is present.
	// menu.add(0, INSERT_ID, 0, R.string.menu_add);
	// return true;
	// }

	// @Override
	// public boolean onOptionsItemSelected(MenuItem item)
	// {
	// // TODO Auto-generated method stub
	// switch (item.getItemId())
	// {
	// case INSERT_ID:
	// startActivity(new Intent(FavoriteList.this, Favorite.class));
	// break;
	//
	// }
	// return super.onOptionsItemSelected(item);
	// }

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int pos, long id) {
		// TODO Auto-generated method stub
		// Bundle bundle = new Bundle();
		// bundle.putLong(KEY_ID, id);
		Intent intent = new Intent(FavoriteList.this, Favorite.class);
		intent.putExtra(KEY_ID, id);
		db.close();
		startActivity(intent);

	}

}
